package com.xx.pmis.web.util;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xx.pmis.model.Social;
import com.xx.pmis.model.SocialBudget;
import com.xx.pmis.model.util.SocialMonth;
import com.xx.pmis.services.SocialBudgetService;
import com.xx.pmis.services.SocialService;

public class SocialUtil extends PMISBaseUtil {
	private static final Log log = LogFactory.getLog(SocialUtil.class);

	SocialBudgetService socialBudgetService = getPMISServices().getSocialBudgetService();
	SocialService socialService = getPMISServices().getSocialService();

	// 最近十二个月的收入支出以及余额
	public List<SocialMonth> getSocialOfTwelveMonths() {
		// 合计
		double socialTotal = 0.00, budgetTotal = 0.00;
		int numTotal = 0;
		String currentDate = CurrentDate.currentDate();
		String currentMonth = currentDate.substring(5, 7);
		String currentYear = currentDate.substring(0, 4);

		int year = Integer.parseInt(currentYear);
		int month = Integer.parseInt(currentMonth);
		String y, m;

		List<SocialMonth> list = new ArrayList<SocialMonth>();
		for (int i = 0; i < 12; i++) {
			double currentSocial = 0.00;
			SocialMonth social = new SocialMonth();
			if (i != 0) {
				month = month - 1;
			}
			if (month == 0) {
				month = 12;
				year = year - 1;
			}

			y = String.valueOf(year);
			m = String.valueOf(month);
			if (m.length() == 1) {
				m = 0 + m;

			}
			// 当前月
			List<Social> all1 = new ArrayList<Social>();
			try {
				all1 = socialService.getAllSocialOfMonth(y, m);
			} catch (Exception e) {
				log.info("【物资管理/社交】根据日期查询该月所有社交记录异常！" + e.toString());
				e.printStackTrace();
			}
			Iterator<?> it1 = all1.iterator();
			while (it1.hasNext()) {
				Social social1 = (Social) it1.next();

				String socialExpense = replaceAllRMB(social1.getSocialExpense());
				if (socialExpense.equals("") || socialExpense.equals(null)) {
					socialExpense = "0.00";
				}
				// 计算当前月支出和
				currentSocial = currentSocial + Double.parseDouble(socialExpense);

			}
			social.setSocialExpense(if1ZeroTo2Zero(new DecimalFormat("####.##").format(currentSocial)));
			social.setSocialDate(y + "-" + m);
			social.setSocialMonth(m);
			social.setNum(all1.size());
			// 预算
			String budgetExpense = "";
			SocialBudget socialBudget;
			try {
				socialBudget = socialBudgetService.getSocialBudgetByBudgetDate(y + "-" + m);
				if(socialBudget == null) {
					budgetExpense = "0.00";
				} else {
					budgetExpense = socialBudget.getBudgetExpense();
				}
				social.setSocialBudget(budgetExpense);
				social.setSocialDesc(socialBudget.getBudgetDesc());
			} catch (Exception e) {
				log.info("【物资管理/社交】根据日期查询该月的社交预算异常！" + e.toString());
				e.printStackTrace();
			}
			if(currentSocial > Double.parseDouble(budgetExpense)) {
				social.setSocialState("超支");
				// 计算超支费用
				social.setOverspend(if1ZeroTo2Zero(new DecimalFormat("####.##").format(Double.parseDouble(budgetExpense) - currentSocial)) + "￥");
			} else {
				social.setSocialState("");
			}
			list.add(social);

			// 实际花费合计
			socialTotal = socialTotal + currentSocial;
			// 花费次数合计
			numTotal = numTotal + all1.size();
			// 预算合计
			budgetTotal = budgetTotal + Double.parseDouble(budgetExpense);
		}

		// 合计
		SocialMonth social2 = new SocialMonth();
		social2.setSocialDate("合计");
		social2.setSocialExpense(if1ZeroTo2Zero(new DecimalFormat("####.##").format(socialTotal)));
		social2.setSocialBudget(if1ZeroTo2Zero(new DecimalFormat("####.##").format(budgetTotal)));
		social2.setSocialDesc("近一年社交支出情况表");
		social2.setSocialMonth("");
		social2.setNum(numTotal);
		
		if(socialTotal > budgetTotal) {
			social2.setSocialState("超支");
			// 计算超支费用
			social2.setOverspend(if1ZeroTo2Zero(new DecimalFormat("####.##").format(budgetTotal - socialTotal)) + "￥");
		} else {
			social2.setSocialState("");
		}
		
		list.add(social2);

		return list;
	}

	/**
	 * 判断金额后面1个0还是2个0，如果1个0或者没有0，则补全到2个0，例如100.1，变成100.10
	 * 
	 * @param str
	 */
	public String if1ZeroTo2Zero(String str) {
		if (str.indexOf(".") != -1) {
			if (str.substring(str.lastIndexOf(".")).length() == 2) {// 1.0
				str = str + "0";
			} else if (str.substring(str.lastIndexOf(".")).length() > 3) {// 1.000,截取小数点后两位
				str = str.substring(0, str.lastIndexOf(".") + 3);
			} else if (str.substring(str.lastIndexOf(".")).length() < 2) {// 1.
				str = str + "00";
			} else {// ==3,1.00
			}
		} else {
			str = str + ".00";
		}
		return str;
	}

	// 把收入支出中的"-"、"￥"剔除掉
	public static String replaceAllRMB(String arg) {
		arg = arg.replaceAll("￥", "").replace("-", "");
		return arg;

	}
}
